<?php
namespace addons\yalimedia\service;

use app\admin\model\yalimedia\course\Content;
use app\admin\model\yalimedia\course\Contentlog;
use app\admin\model\yalimedia\course\Unit;
use app\admin\model\yalimedia\Fav;
use app\admin\model\yalimedia\Uservip;

class Course
{

    public $model;
    public $error;

    public function __construct()
    {
        $this->model = new \app\admin\model\yalimedia\Course();
    }

    /**
     * 获取课程列表
     * @param $page
     * @param $limit
     * @throws \think\Exception
     */
    public function getCourseList($where,$page,$limit){
        $count = $this->model->where($where)->where(['status'=>1])->count();
        if($page){
            $data = $this->model->where($where)->where(['status'=>1])->page($page,$limit)->order("weigh asc")->select();
        }else{
            $data = $this->model->where($where)->where(['status'=>1])->order("weigh asc")->select();
        }

        return ['total'=>$count,'data'=>$data];
    }

    /**
     * 获取热门课程列表
     * @param $page
     * @param $limit
     * @throws \think\Exception
     */
    public function getHotCourseList($page,$limit){
        $where = ['is_hot'=>1,'status'=>1];
        $count = $this->model->where($where)->count();
        if($page){
            $data = $this->model->where($where)->page($page,$limit)->order("weigh asc")->select();
        }else{
            $data = $this->model->where($where)->order("weigh asc")->select();
        }

        return ['total'=>$count,'data'=>$data];
    }

    /**
     * 获取课程详情
     */
    public function getCourseInfo($id,$user_id){

        //判断当前用户的类型
        $courseModel = new \app\admin\model\yalimedia\Course();
        $courseInfo = $courseModel->where(['id'=>$id,'status'=>1])->field("id,title,price,vip_price,cover,content,teacher_id,createtime")->find();
        if(empty($courseInfo)){
            $this->error = "当前课程不存在";
            return false;
        }

        //完善接口连接
        $courseInfo['cover'] = request()->domain().$courseInfo['cover'];
        $courseInfo['content']=preg_replace('#src="/#is', 'src="'.request()->domain().'/',$courseInfo['content']);

        //完善教师信息
        $teacherModel = new \app\admin\model\yalimedia\Teacher();
        $teacherInfo = $teacherModel->where(['id'=>$courseInfo['teacher_id']])->find();
        if(!empty($teacherInfo)){
            $teacherInfo['image'] = request()->domain()."/".$teacherInfo['image'];
            $teacher = new Teacher();
            $teacherInfo['course_num'] = $teacher->getCourseNum($courseInfo['teacher_id']);
            $teacherInfo['study_num'] = $teacher->getStudyNum($courseInfo['teacher_id']);
        }
        $courseInfo['teacher'] = $teacherInfo;

        //计算课时
        $courseUnitModel = new Unit();
        $unitList = collection($courseUnitModel->where(['course_id'=>$id])->order("weigh asc")->select())->toArray();

        $courseContentModel = new Content();
        $courseContentList = collection($courseContentModel
            ->alias("content")
            ->field("content.*,IFNULL(log.id,0) as study_status")
            ->join('yalimedia_course_content_log log',"content.id=log.content_id and log.user_id=".$user_id,'left')
            ->where(['content.course_id'=>$id])
            ->order("content.weigh asc")
            ->select())->toArray();


        $contentLogModel = new Contentlog();
        if($lastContent = $contentLogModel->where(['course_id'=>$id,'user_id'=>$user_id])->order("last_time desc")->find()){
            $last_content_id = $lastContent['content_id'];
        }else{
            $last_content_id = "";
        }

        $course_num = 0 ;
        foreach($unitList as &$v){
            $course = [];
            foreach($courseContentList as &$vv){
                if(!empty($vv['study_status'])){
                    $vv['study_status'] = 1;
                }else{
                    $vv['study_status'] = 0;
                }

                //判断是否为最后播放的节
                if($vv['id'] == $last_content_id && $last_content_id){
                    $vv['is_last'] = 1;
                }else{
                    $vv['is_last'] = 0;
                }

                //组合章节
                if($vv['unit_id'] == $v['id']){
                    $course[] = $vv;
                }

            }
            $v['course'] = $course;
            $course_num += count($course);
        }

        $courseInfo['unit'] = $unitList;
        $courseInfo['course_count'] = $course_num;


        $favModel = new Fav();
        if($favModel->where(['type'=>1,'item_id'=>$id,'user_id'=>$user_id])->find()){
            $courseInfo['is_fav'] = 1;
        }else{
            $courseInfo['is_fav'] = 0;
        }

        /**
         * 是否购买
         */
        $order = new \app\admin\model\yalimedia\Order();
        if($order->where(['type'=>1,'status'=>2,'item_id'=>$id,'user_id'=>$user_id])->find()){
            $courseInfo['is_buy'] = 1;
        }else{
            $courseInfo['is_buy'] = 0;
        }

        //判断是否为会员
        $courseInfo['is_vip'] = User::isVip($user_id);

        //获取学习人数
        $contentLogModel = new Contentlog();
        $courseInfo['study_count'] = $contentLogModel
            ->alias("log")
            ->join("yalimedia_course course","log.course_id=course.id")
            ->where(['log.course_id'=>$id,'course.status'=>1])
            ->group("log.user_id")
            ->count();

        return $courseInfo;
    }

    /**
     * 获取学习记录列表
     * @param $page
     * @param $limit
     * @throws \think\Exception
     */
    public function getContentLogList($user_id,$page,$limit){

        $contentLogModel = new Contentlog();
        $count = $contentLogModel
            ->alias("log")
            ->join("yalimedia_course course","log.course_id=course.id")
            ->where(['log.user_id'=>$user_id,'course.status'=>1])
            ->group("log.course_id")
            ->count();

        if($page){
            $data = $contentLogModel
                ->alias("log")
                ->field("course.*")
                ->join("yalimedia_course course","log.course_id=course.id")
                ->where(['log.user_id'=>$user_id,'course.status'=>1])
                ->group("log.course_id")
                ->page($page,$limit)->order("log.last_time desc")->select();
        }else{
            $data = $contentLogModel
                ->alias("log")
                ->field("course.*")
                ->join("yalimedia_course course","log.course_id=course.id")
                ->where(['log.user_id'=>$user_id,'course.status'=>1])
                ->group("log.course_id")
                ->order("log.last_time desc")->select();
        }

        return ['total'=>$count,'data'=>$data];
    }


}